import { MAO_CAI_SHEN, HALF_GOAL, initialState, RIGHT_SCORE, RIGHT_SCORE_V2, BOTH_GOAL, WIN_DRAW_LOSE } from "../../const/index";
import { containsAnyCharacter } from "../index";
import { getOrderMoney, getRightGoal, getWordAndKey } from "./util";

export const getRightPankouWith = ({
  texts,
  zhuboMapWords,
  noReal2RealMaps,
  zhuName,
  keName,
}: {
  texts: string[];
  zhuboMapWords: any;
  noReal2RealMaps: any;
  zhuName: string;
  keName: string
}) => {
  let rightPankouItems = [];
  for (let i = 0; i < texts.length; i++) {
    const text = texts[i];
    let findWord, findKey;
    const bifengRegex = /(\d+:\d+|\d+-\d+|\d+：\d+)/g;

    if (!text.includes("【") && !text.includes("】")) {
      continue;
    }

    if (initialState.zhuboming === MAO_CAI_SHEN) {
      if (text?.startsWith("【角")) {
        continue;
      }
    }

    // 只有猫哥才能走到这里，如果需要兼容其他主播，需要再优化
    let middleText = text;
    // 【大2.5球】 匹配中间的文本 大2.5球
    const pattern = /【([^】]+)】/;
    const match = text.match(pattern);
    if (match?.[1]) {
      middleText = match?.[1];
    }

    // 让球前面的比分
    let rangqiuQianBifen = "";
    const haveZhuKe = ["主", "客"].some((item) => middleText.includes(item));
    const haveJiaJian = ["+", "-"].some((item) => middleText.includes(item));
    if (haveZhuKe && haveJiaJian) {
      rangqiuQianBifen = middleText.match(bifengRegex)?.[0] || "";
      middleText = middleText.replace(bifengRegex, "");
    }

    // 将 2.5 替换成 n
    const reg = /\d+(?:\.\d+)?/;
    let pankouNum;
    const textToN = middleText.replace(reg, "n");
    const match2 = middleText.match(reg);
    if (match2?.[0]) {
      pankouNum = match2?.[0];
    }
    pankouNum = getRightGoal(pankouNum || '0');

    // 匹配下单金额
    const pay = getOrderMoney(text);
    if (!pay) {
      continue;
    }

    if (text === "半破") {
      rightPankouItems.push({
        key: HALF_GOAL,
        text: "大 0.5",
        pankouNum,
        pay,
      });
      continue;
    }

    // 找到对应盘口的 一级分类（大/小）和 具体下单的item
    const { findWord: _word, findKey: _key } = getWordAndKey(textToN, zhuboMapWords, noReal2RealMaps);
    findWord = findWord ? findWord : _word;
    findKey = findKey ? findKey : _key;

    console.log(findWord, findKey, 'findWordfindKey');

    // 匹配比分
    if (
      bifengRegex.test(middleText) &&
      !containsAnyCharacter(middleText, ["主", "客"])
    ) {
      const match3 = middleText.match(bifengRegex);

      if (
        middleText.includes("2-3") ||
        middleText.includes("4-6") ||
        middleText.includes("球")
      ) {
        // 进球数的盘口 暂时不做
        rightPankouItems.push({
          key: "总进球数",
          text: match3?.[0],
          pankouNum: 0,
          pay,
          rangqiuQianBifen,
        });
        continue;
      } else {
        if ((match3 as any).length > 0) {
          rightPankouItems.push({
            key: RIGHT_SCORE,
            scores: match3,
            pay,
          });
          rightPankouItems.push({
            key: RIGHT_SCORE_V2,
            scores: match3,
            pay,
          });
          continue;
        }
      }
    }

    // 如果盘口带有数字
    if (pankouNum) {
      if (findWord?.includes("大")) {
        findWord = `大 ${pankouNum}`;
      }
      if (findWord?.includes("小")) {
        findWord = `小 ${pankouNum}`;
      }
    }

    // 如果是让xx
    // 主-n牌子 => 利物浦-0.5/1
    if (findWord?.includes("主")) {
      if (Number(pankouNum) == 0) {
        findWord = `${zhuName} 0`;
      } else {
        if (findWord?.includes("+")) {
          findWord = zhuName + " +" + pankouNum;
        }
        if (findWord?.includes("-")) {
          findWord = zhuName + " -" + pankouNum;
        }
      }
    }
    if (findWord?.includes("客")) {
      if (Number(pankouNum) == 0) {
        findWord = `${keName} 0`;
      } else {
        if (findWord?.includes("+")) {
          findWord = keName + " +" + pankouNum;
        }
        if (findWord?.includes("-")) {
          findWord = keName + " -" + pankouNum;
        }
      }
    }

    // 如果是双进
    if (findKey?.includes(BOTH_GOAL)) {
      if (!findWord?.includes("否")) {
        findWord = "是";
      } else {
        findWord = "否";
      }
    }

    // 胜平负
    if (findKey === WIN_DRAW_LOSE) {
      if (text.includes("主")) {
        findWord = "主";
      }
      if (text.includes("客")) {
        findWord = "客";
      }
      if (text.includes("平")) {
        findWord = "平";
      }
    }

    // 如果没有pay就不去下单？
    rightPankouItems.push({
      key: findKey,
      text: findWord,
      pankouNum: pankouNum || 0,
      pay,
      rangqiuQianBifen,
    });
  }

  // 处理比分的情况
  const scoreResults: {
    key: string;
    text: string;
    pay: string;
  }[] = [];
  rightPankouItems = rightPankouItems
    .map((it: any) => {
      if (it.key === RIGHT_SCORE || it.key === RIGHT_SCORE_V2) {
        it.scores.forEach((it2: any) => {
          if (it.pay >= 5) {
            scoreResults.push({
              key: it.key,
              text: it2.replace(":", "-").replace("：", "-"),
              pay: it.pay,
            });
          }
        });
        return false;
      } else {
        return it;
      }
    })
    .filter((item) => item);

  rightPankouItems = [...rightPankouItems, ...scoreResults];
  console.log({ rightPankouItems });
  return rightPankouItems;
}